<?php
class ProfessionalRecordsController extends AppController {

	var $name = 'ProfessionalRecords';

	function index() {
		$this->ProfessionalRecord->recursive = 0;
		$this->set('professionalRecords', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid professional record', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('professionalRecord', $this->ProfessionalRecord->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->ProfessionalRecord->create();
			if ($this->ProfessionalRecord->save($this->data)) {
				$this->Session->setFlash(__('The professional record has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The professional record could not be saved. Please, try again.', true));
			}
		}
		$cvs = $this->ProfessionalRecord->Cv->find('list');
		$this->set(compact('cvs'));
	}

        function add_by_label($label, $date_deb, $date_end, $company, $kind) {
            if (!empty($label)) {
                $this->ProfessionalRecord->create();
                $category['ProfessionalRecord']['job'] = $label;
                $category['ProfessionalRecord']['started_at'] = $date_deb;
                $category['ProfessionalRecord']['ended_at'] = $date_end;
                $category['ProfessionalRecord']['company'] = $company;
                $category['ProfessionalRecord']['kind'] = $kind;
                if ($this->ProfessionalRecord->save($category)) {
                    return $this->ProfessionalRecord->id;
                } else {
                        return null;
                }
            }
            return null;
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid professional record', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->ProfessionalRecord->save($this->data)) {
				$this->Session->setFlash(__('The professional record has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The professional record could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->ProfessionalRecord->read(null, $id);
		}
		$cvs = $this->ProfessionalRecord->Cv->find('list');
		$this->set(compact('cvs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for professional record', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->ProfessionalRecord->delete($id)) {
			$this->Session->setFlash(__('Professional record deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Professional record was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

        function delete_by_cv($id = null) {

            if(!empty ($id)) :
                $idProfRec = $this->ProfessionalRecord->query('SELECT *
                                                       FROM professional_records_cvs pc
                                                       WHERE cv_id ='.$id);
                if(count($idProfRec)>0) :
                    foreach ($idProfRec as $exp) :
                        $this->ProfessionalRecord->delete(array('id' => $exp['pc']['professional_record_id']));
                    endforeach;

                    $this->ProfessionalRecord->query('DELETE FROM professional_records_cvs
                                           WHERE cv_id ='.$id);
                endif;
            endif;
        }

	function admin_index() {
		$this->ProfessionalRecord->recursive = 0;
		$this->set('professionalRecords', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid professional record', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('professionalRecord', $this->ProfessionalRecord->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->ProfessionalRecord->create();
			if ($this->ProfessionalRecord->save($this->data)) {
				$this->Session->setFlash(__('The professional record has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The professional record could not be saved. Please, try again.', true));
			}
		}
		$cvs = $this->ProfessionalRecord->Cv->find('list');
		$this->set(compact('cvs'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid professional record', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->ProfessionalRecord->save($this->data)) {
				$this->Session->setFlash(__('The professional record has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The professional record could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->ProfessionalRecord->read(null, $id);
		}
		$cvs = $this->ProfessionalRecord->Cv->find('list');
		$this->set(compact('cvs'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for professional record', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->ProfessionalRecord->delete($id)) {
			$this->Session->setFlash(__('Professional record deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Professional record was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>